package com.example.springaop.Aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 666
 * Date: 2023-08-20
 * Time: 17:17
 */
@Aspect    //表明这个类为一个切面
@Component
public class UserAspect {
    //定义切点,使⽤ AspectJ 表达式语法
    @Pointcut("execution(* com.example.springaop.Controller.UserController.* (..))")
    public void pointcut() {
    }

    // 前置通知
    @Before("pointcut()")
    public void doBefore() {
        System.out.println("执⾏ Before ⽅法");
    }

    // 后置通知
    @After("pointcut()")
    public void doAfter() {
        System.out.println("执⾏ After ⽅法");
    }

    @AfterReturning("pointcut()")
    public void doAfterReturning() {
        System.out.println("执⾏ return 之前通知⽅法");
    }

    @AfterThrowing
    public void doAfterThrowing() {
        System.out.println("执行异常抛出之前的通知方法");
    }

    @Around("pointcut()")
    public Object doAround(ProceedingJoinPoint joinPoint) {
        Object obj = new Object();

        System.out.println("开始执行环绕通知方法");
        try {
            //执行目标方法
            joinPoint.proceed();
        } catch (Throwable e) {
            e.printStackTrace();
        }
        System.out.println("环绕通知方法结束");
        return obj;
    }


}
